#include <QtGui/QApplication>
#include "mainwindow.h"
#include "library.h"
#include <iostream>
#include "writer.h"
#include "reader.h"
#include <QRunnable>
#include <QThreadPool>


int main(int argc, char *argv[])
{
    if (argc < 6) {
        std::cout << "Pass read time, write time, library size, readers count, writers count" << std::endl;
        return 0;
    }
    int readTime = atoi(argv[1]);
    int writeTime = atoi(argv[2]);
    int libSize = atoi(argv[3]);
    int readersCount = atoi(argv[4]);
    int writersCount = atoi(argv[5]);

    QApplication a(argc, argv);
    MainWindow w;


    Library * lib = new Library(libSize);

    w.setCentralWidget(lib);

    QThreadPool * pool = new QThreadPool(&w);
    pool->setMaxThreadCount(2*(readersCount + writersCount));



    for(int i = 0; i < readersCount; ++i){
        pool->start(new Reader(lib, readTime));
        usleep(readTime / readersCount);
    }

    for(int i = 0; i < writersCount; ++i){
        pool->start(new Writer(lib, writeTime));
        usleep(writeTime / writersCount);
    }


    w.show();

    int execResult =  a.exec();

    lib->close();
    pool->waitForDone();

    delete pool;
    delete lib;

    return execResult;
}
